package mainpackage;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.io.*;
import java.util.*;

public class HOS {
	private ArrayList<Item> itemList;
	private ArrayList<Room> roomList;
	private Order currentOrder;
	private Category filter;
	private ArrayList<Item> filteredItemList;

	public HOS() {
		itemList = new ArrayList<Item>();
		roomList = new ArrayList<Room>();
		currentOrder = new Order();
		filter = null;
		filteredItemList = new ArrayList<Item>();
	}

	public Order getCurrentOrder() {
		return currentOrder;
	}

	public void setCurrentOrderNull() {
		this.currentOrder = null;
	}

	public void loadItemList(String itemInput) throws FileNotFoundException {

		java.io.File file = new java.io.File(itemInput);
		if (!file.exists()) {
			System.out.println("item list file " + itemInput
					+ " does not exist");
			System.exit(0);
		}

		/* input data from file */
		java.util.Scanner input = new java.util.Scanner(file);
		while (input.hasNext()) {
			Item item = new Item();
			item.setId(input.nextLine());
			item.setName(input.nextLine());
			item.setPrice(Double.parseDouble(input.nextLine()));
			item.setCategory(input.nextLine());
			String s = input.nextLine();
			itemList.add(item);
		}
	}

	public void loadRoomList(String roomInput) throws FileNotFoundException {
		java.io.File file = new java.io.File(roomInput);
		if (!file.exists()) {
			System.out.println("room list file " + roomInput
					+ " does not exist");
			System.exit(0);
		}

		/* input data from file */
		java.util.Scanner input = new java.util.Scanner(file);
		while (input.hasNext()) {
			Room room = new Room();
			room.setRoomNr(Integer.parseInt(input.nextLine()));
			room.setOccupant(input.nextLine());
			room.setNrAdults(Integer.parseInt(input.nextLine()));
			room.setNrChildren(Integer.parseInt(input.nextLine()));
			String s = input.nextLine();
			roomList.add(room);
		}
	}

	public void createNewOrder(String roomId) {
		Room room = new Room();

		for (int i = 0; i < roomList.size(); i++) {
			if (Integer.parseInt(roomId) == roomList.get(i).getRoomNr()) {
				room = roomList.get(i);
			}
		}
		currentOrder.setRoom(room);
	}

	public void createNewOrder(int roomId) {
		Room room = new Room();

		for (int i = 0; i < roomList.size(); i++) {
			if (roomId == roomList.get(i).getRoomNr()) {
				room = roomList.get(i);
			}
		}
		currentOrder.setRoom(room);
	}

	public void setFilter(Category category) {
		filter = category;
	}

	public void removeFilter() {
		filter = null;
	}

	public ArrayList<Item> getFilteredItemList() {
		filteredItemList.clear();
		if (filter == null) {
			return itemList;
		} else {
			for (int i = 0; i < itemList.size(); i++) {
				if (filter == itemList.get(i).getCategory()) {
					filteredItemList.add(itemList.get(i));
				}// if
			}// for
		}// if/else

		return filteredItemList;
	}

	public void removeItemFromOrder(int i) {
		currentOrder.getItems().remove(i);
	}

	public void addItemToOrder(Item item) {
		currentOrder.getItems().add(item);
	}

	public void saveOrder() throws IOException {

		String filename, date, date1, time, time1;
		double total;
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf2 = new SimpleDateFormat("hh-mm-ss");
		SimpleDateFormat sdf3 = new SimpleDateFormat("dd-MM-yyyy");
		SimpleDateFormat sdf4 = new SimpleDateFormat("hh:mm");
		Date now = new Date();
		date = sdf1.format(now).toString();
		time = sdf2.format(now).toString();
		date1 = sdf3.format(now).toString();
		time1 = sdf4.format(now).toString();

		filename = "MealOrder-Room-" + currentOrder.getRoom().getRoomNr()
				+ "-Date-" + date + "-Time-" + time + ".txt";

		java.io.File file = new java.io.File(filename);
		if (!file.exists()) {
			// System.out.println("order file does not exist,then create it!");
			file.createNewFile();
		}

		java.io.PrintWriter output = new java.io.PrintWriter(file);

		output.println(date1 + "  " + time1);
		output.println("Room Number:  " + currentOrder.getRoom().getRoomNr());
		output.println("Food items ordered:");
		total = 0;
		for (int i = 0; i < currentOrder.getItems().size(); i++) {
			output.println("    " + currentOrder.getItems().get(i).getName()
					+ " $" + currentOrder.getItems().get(i).getPrice());
			total += currentOrder.getItems().get(i).getPrice();
		}
		output.println("Total: $" + total);
		output.close();
	}

	public void displayState() {
		System.out.println("HOS");
		/* Display item list */
		System.out.println("          " + "itemList: [");
		int i;
		for (i = 0; i < itemList.size(); i++) {
			System.out.print("                        Item[");
			System.out.print("code: " + itemList.get(i).getId() + ", ");
			System.out.print("name: " + itemList.get(i).getName() + ", ");
			System.out.print("price: " + itemList.get(i).getPrice() + ", ");
			System.out.println("category: " + itemList.get(i).getCategory()
					+ "]");
		}
		System.out.println("                    ]\n");

		/* Display room list */
		System.out.println("          " + "roomList: [");
		for (i = 0; i < roomList.size(); i++) {
			System.out.print("                        Room[");
			System.out.print("roomNr: " + roomList.get(i).getRoomNr() + ", ");
			System.out.print("occupant: " + roomList.get(i).getOccupant()
					+ ", ");
			System.out.print("nrAdults: " + roomList.get(i).getNrAdults()
					+ ", ");
			System.out.println("nrChildren: " + roomList.get(i).getNrChildren()
					+ "]");
		}
		System.out.println("                    ]\n");

		/* Display current order */
		System.out.println("          " + "currentOrder: ");
		System.out.println("                    [");
		System.out.println("                    Order: ");
		System.out.print("                    room: [");
		System.out
				.print("roomNr: " + currentOrder.getRoom().getRoomNr() + ", ");
		System.out.print("occupant: " + currentOrder.getRoom().getOccupant()
				+ ", ");
		System.out.print("nrAdults: " + currentOrder.getRoom().getNrAdults()
				+ ", ");
		System.out.println("nrChildren: "
				+ currentOrder.getRoom().getNrChildren() + "]");

		System.out.println("                    items: ");
		for (i = 0; i < currentOrder.getItems().size(); i++) {
			System.out.print("                      Item[");
			System.out.print("code: " + currentOrder.getItems().get(i).getId()
					+ ", ");
			System.out.print("name: "
					+ currentOrder.getItems().get(i).getName() + ", ");
			System.out.print("price: "
					+ currentOrder.getItems().get(i).getPrice() + ", ");
			System.out.println("category: "
					+ currentOrder.getItems().get(i).getCategory() + "]");
		}
		System.out.println("                    ]\n");

		/* Display filter */
		System.out.println("          filter:" + filter + "\n");

		/* Display filteredItemList */
		System.out.println("          " + "fiteredItemList: ");
		System.out.println("                    [");
		for (i = 0; i < getFilteredItemList().size(); i++) {
			System.out.print("                        Item[");
			System.out.print("code: " + getFilteredItemList().get(i).getId()
					+ ", ");
			System.out.print("name: " + getFilteredItemList().get(i).getName()
					+ ", ");
			System.out.print("price: "
					+ getFilteredItemList().get(i).getPrice() + ", ");
			System.out.println("category: "
					+ getFilteredItemList().get(i).getCategory() + "]");
		}
		System.out.println("                    ]");
	}

}
